/*登陆 */

'use strict';

define(['angular'], function (angular) {
    angular.module('login',[])
    .controller('LoginCtrl', ['$scope','$http','$state',function ($scope,$http,$state) {
        $scope.userInfo = {};
        $scope.loginFailedCnt=0;
        $scope.checkRequire = false;

        $scope.login = function(){
            /*if ($scope.checkRequire == true){
                if(!$scope.validate()){
                    return;
                }
            }*/
            $http.get('/user/getByEmail?email=' + $scope.userInfo.identifier)
                .success(function (data){
                    /*var role = data.role;
                    if(role){
                        $cookieStore.put("identifier",$scope.userInfo.identifier);
                        $cookieStore.put("password",$scope.userInfo.password);
                        $cookieStore.put("role",data.role);
                        if ($scope.userInfo.autoLogin){
                            $cookieStore.put("autoLogin",true);
                        }
                        $state.go('main.home' ,{userPower:data.role});
                    }else{
                        $scope.loginFailedCnt = $scope.loginFailedCnt +1;
                        $scope.createCheckCode();
                    }*/
                    alert("欢迎你:" + data.userName);
                    $state.go('main');
                })
                .error(function (data){
                    $scope.showDangerMsg(data.error);
                    $scope.loginFailedCnt = $scope.loginFailedCnt +1;
                    $scope.createCheckCode();
                });
        };
        /*$scope.createCheckCode =function(){
            if ($scope.loginFailedCnt >= 5){
                $scope.checkRequire = true;
                //产生验证码
                var code = "";
                //验证码的长度
                var codeLength = 4;
                var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');//随机数
                //循环操作
                for(var i = 0; i < codeLength; i++) {
                    //取得随机数的索引（0~35）
                    var index = Math.floor(Math.random()*36);
                    //根据索引取得随机数加到code上
                    code += random[index];
                }
                //把code值赋给验证
                $scope.checkCode = code;
            }
        };
        $scope.validate = function(){
            //取得输入的验证码并转化为大写
            var inputCode = $scope.userInfo.inputCode;
            //若输入的验证码长度为0
            if(typeof(inputCode) == "undefined" || inputCode.length <= 0) {
                //则弹出请输入验证码
                $scope.showInfoMsg("请输入验证码！");
                return false;
            }
            inputCode = inputCode.toUpperCase();
            //若输入的验证码与产生的验证码不一致时
            if(inputCode != $scope.checkCode ) {
                //则弹出验证码输入错误
                $scope.showInfoMsg("验证码输入错误！");
                //刷新验证码
                $scope.createCheckCode();
                //清空文本框
                $scope.userInfo.inputCode = "";
                return false;
            }else {
                //输入正确时
                return true;
            }
        };
        $scope.forgetPassword =function(){
            $scope.forgetPass = true;
        };
        $scope.sendCheckEmail =function(){
            $scope.checkEmail = $scope.userInfo.checkEmail;
            $http.post('/reset', {email:$scope.checkEmail})
                .success(function (data){
                    $scope.sendEmailBoo = true;
                })
                .error(function (data){
                });
        };
        //是否进行自动登陆
        if ($cookieStore.get("autoLogin")){
            $scope.userInfo.identifier = $cookieStore.get("identifier");
            $scope.userInfo.password = $cookieStore.get("password");
            $scope.userInfo.autoLogin = $cookieStore.get("autoLogin");
        }
        $scope.showSuccessMsg=function(successMsg){
            $scope.successBoo = true;
            $scope.successMsg = successMsg;
            $timeout(function(){
                $scope.successBoo = false;
            },3000);
        };
        $scope.showDangerMsg=function(dangerMsg){
            $scope.dangerBoo = true;
            $scope.dangerMsg = dangerMsg;
            $timeout(function(){
                $scope.dangerBoo = false;
            },3000);
        };
        $scope.showInfoMsg=function(infoMsg){
            $scope.infoBoo = true;
            $scope.infoMsg = infoMsg;
            $timeout(function(){
                $scope.infoBoo = false;
            },3000);
        };*/
    }])
});
